package com.alfred.mall.order.application.controller;

import com.alfred.common.framwork.util.HttpUtil;
import com.alfred.common.framwork.vo.CommonResult;
import com.alfred.mall.order.api.OrderService;
import com.alfred.mall.order.api.bo.OrderCreateBO;
import com.alfred.mall.order.api.bo.OrderPageBO;
import com.alfred.mall.order.api.dto.OrderCreateDTO;
import com.alfred.mall.order.api.dto.OrderQueryDTO;
import com.alfred.mall.order.api.po.OrderCreatePO;
import com.alfred.mall.order.application.convert.OrderConvertAPP;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * 订单API(users)
 */
@RestController
@RequestMapping("/users/order")
@Validated
public class OrderController {

    @Reference(validation = "true", version = "${dubbo.provider.OrderService.version}")
    OrderService orderService;


    /**
     * 查询订单信息
     * @param orderQueryDTO
     * @return
     */
    @GetMapping("/order_page")
    public CommonResult<OrderPageBO> getOrderPage(@Valid OrderQueryDTO orderQueryDTO) {
        Integer userId = 11;
        orderQueryDTO.setUserId(userId);
        return orderService.getOrderPage(orderQueryDTO);
    }

    /**
     * 创建订单
     * @param orderCreatePO
     * @param request
     * @return
     */
    @PostMapping("create_order")
    public CommonResult<OrderCreateBO> createOrder(@RequestBody @Validated OrderCreatePO orderCreatePO,
                                                   HttpServletRequest request) {
        Integer userId = 11;
        OrderCreateDTO orderCreateDTO = OrderConvertAPP.INSTANCE.convert(orderCreatePO);
        orderCreateDTO.setUserId(userId).setIp(HttpUtil.getIp(request));
        return orderService.createOrder(orderCreateDTO);
    }

}
